package com.zzyl.controller;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.RoleDto;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/role")
@Api("角色管理")
public class RoleController {
    @Autowired
    RoleService roleService;

    /**
     * 角色添加
     * @param roleDto
     * @return
     */
    @ApiOperation("角色添加")
    @PutMapping
    public ResponseResult add(@RequestBody RoleDto roleDto) {
        log.info("角色添加: {}",roleDto);
        roleService.add(roleDto);
        return ResponseResult.success();
    }

    /**
     * 角色分页
     * @param roleDto
     * @return
     */
    @ApiOperation("角色分页")
    @PostMapping("/page/{pageNum}/{pageSize}")
    public ResponseResult<PageResponse<RoleVo>> page(@RequestBody RoleDto roleDto, @PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        log.info("角色分页: {},{},{}",roleDto,pageNum,pageSize);
        PageResponse<RoleVo> pageResponse = roleService.page(roleDto, pageNum, pageSize);
        return ResponseResult.success(pageResponse);
    }

    /**
     * 删除角色
     * @param roleIds
     * @return
     */
    @ApiOperation("删除角色")
    @DeleteMapping("/{roleIds}")
    public ResponseResult deleteById(@PathVariable String roleIds) {
        log.info("删除角色: {}",roleIds);
        roleService.deleteById(roleIds);
        return ResponseResult.success();
    }

    /**
     * 角色修改
     * @return
     */
    @ApiOperation("角色修改")
    @PatchMapping
    public ResponseResult<Boolean> update(@RequestBody RoleDto roleDto) {
        log.info("角色修改: {}",roleDto);
        boolean result = roleService.update(roleDto);
        return ResponseResult.success(result);
    }

    /**
     * 根据角色查询选中的资源数据
     * @return
     */
    @ApiOperation("根据角色查询选中的资源数据")
    @GetMapping("/find-checked-resources/{roleId}")
    public ResponseResult<String[]> getResourcesIdByRoleId(@PathVariable Long roleId) {
        log.info("根据角色查询选中的资源数据: {}",roleId);
        String[] resourcesIds = roleService.getResourcesNoByRoleId(roleId);
        return ResponseResult.success(resourcesIds);
    }

    /**
     * 角色下拉框
     * @return
     */
    @ApiOperation("角色下拉框")
    @PostMapping("/init-roles")
    public ResponseResult<RoleVo> getAll() {
        log.info("角色下拉框");
        List<RoleVo> roleVoList = roleService.getAll();
        return ResponseResult.success(roleVoList);
    }
}
